//
//  WelcomeViewController.m
//  FreshBee
//
//  Created by Goofy on 15/10/2016.
//  Copyright © 2016 iOS013. All rights reserved.
//

#import "WelcomeViewController.h"
#import "TabBarViewController.h"
#define KSize [UIScreen mainScreen].bounds.size
@interface WelcomeViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong) UIScrollView *scrollView;

@property (nonatomic,strong) UIPageControl *pageControl;

@end

@implementation WelcomeViewController


-(UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    }
    return _scrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI{
    
    [self.view addSubview:self.scrollView];
    
    self.scrollView.delegate = self;
    
    _scrollView.contentSize = CGSizeMake(KSize.width*4, KSize.height);

    for (int i = 0; i < 4; i++)
    {
        CGFloat x = i *KSize.width;
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, KSize.width, KSize.height)];
        [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"guide_40_%d",i+1]]];
        [_scrollView addSubview:imageView];
        if (i == 3) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(375*3+375*0.5-50, 550, 100, 32)];
            [_scrollView addSubview:btn];
            [btn setBackgroundImage:[UIImage imageNamed:@"icon_next"] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(removeSub) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    
    self.pageControl = [[UIPageControl alloc] init];
    self.pageControl.numberOfPages = 4;
    self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    [self.view addSubview:self.pageControl];
    [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-30);
    }];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (decelerate == NO) {
        // 1.计算页码
        int page = scrollView.contentOffset.x / scrollView.frame.size.width;
        
        // 2.设置页码
        self.pageControl.currentPage = page;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 1.计算页码
    int page = scrollView.contentOffset.x / scrollView.frame.size.width;
    
    // 2.设置页码
    self.pageControl.currentPage = page;
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 计算页码
    int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);  // 加0.5方便四舍五入
    
    // 设置页码
    self.pageControl.currentPage = page;
    
}

-(void)removeSub{

    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[TabBarViewController alloc]init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
